package com.ruoyi.background.domain;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class AppLotteryExport {

    private static final Logger log = LoggerFactory.getLogger(AppLotteryExport.class);

    private final XSSFWorkbook workbook;

    private final List<String> heads;

    private final List<String> countHeads;

    private XSSFSheet sheet;

    private SimpleDateFormat simpleDateFormat;


    /**
     * 创建sheet
     */
    public void createSheet(String deptName) {
        this.sheet = this.workbook.createSheet(deptName);
    }

    /**
     * 导出excel 到响应流
     *
     * @param response 响应流
     */
    public void excelDownload(HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        try {
            this.workbook.write(response.getOutputStream());
        } catch (Exception e) {
            log.error("导出Excel异常{}", e.getMessage());
        } finally {
            IOUtils.closeQuietly(this.workbook);
        }
    }


    /**
     * 写入统计的数据
     *
     * @param list 统计的数据
     */
    public void writeCountData(List<AppLotteryStatistics> list) {
        int rowNum = 2; // 行号
        int order = 0;
        int allSum = 0;
        for (AppLotteryStatistics record : list) {
            order++;
            allSum = allSum + record.getNum();
            Row temp = this.sheet.createRow((rowNum++));
            Cell c = temp.createCell(0);
            c.setCellValue(order);
            c = temp.createCell(1);
            c.setCellValue(record.getDeptName());
            c = temp.createCell(2);
            c.setCellValue(record.getNum());
        }
        Row temp = this.sheet.createRow((rowNum++));
        Cell c = temp.createCell(0);
        c.setCellValue(order);
        c = temp.createCell(1);
        c.setCellValue("合计");
        c = temp.createCell(2);
        c.setCellValue(allSum);
    }


    /**
     * 写入数据
     *
     * @param records 数据
     */
    public void writeListData(List<AppLotteryRecord> records) {
        int rowNum = 2; // 行号
        for (AppLotteryRecord record : records) {
            Row temp = this.sheet.createRow((rowNum++));
            Cell c = temp.createCell(0);
            c.setCellValue(record.getUserId());
            c = temp.createCell(1);
            c.setCellValue(record.getDeptName());
            c = temp.createCell(2);
            c.setCellValue(record.getUserName());
            c = temp.createCell(3);
            c.setCellValue(record.getPhonenumber());
            c = temp.createCell(4);
            c.setCellValue("第" + record.getLotteryIndex() + "轮");
            c = temp.createCell(5);
            String format = simpleDateFormat.format(record.getCreateTime());
            c.setCellValue(format);
            c = temp.createCell(6);
            c.setCellValue(record.getPrizeName());
            c = temp.createCell(7);
            c.setCellValue(record.getPrizeNum());
            c = temp.createCell(8);
            c.setCellValue(record.getPrizeCode());
        }
    }


    /**
     * 写入统计的头部
     */
    public void writeCountHead() {
        XSSFCellStyle style = this.workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER); //居中
        style.setWrapText(true); //自动换行
        style.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); //背景颜色
        XSSFFont font = this.workbook.createFont();
        font.setFontName("Arial");
        font.setBold(true);
        style.setFont(font);
        //创建第二行用来放表头文字
        int rowNum = 1; // 行号
        XSSFRow row1 = sheet.createRow(rowNum++);
        //表头数据,循环将表头数据填充到第二行
        for (int i = 0; i < this.countHeads.size(); i++) {
            XSSFCell c1 = row1.createCell(i);
            c1.setCellValue(this.countHeads.get(i));
            c1.setCellStyle(style);
        }
    }


    /**
     * 写入表头
     */
    public void writeHead() {
        XSSFCellStyle style = this.workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER); //居中
        style.setWrapText(true); //自动换行
        style.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); //背景颜色
        XSSFFont font = this.workbook.createFont();
        font.setFontName("Arial");
//        font.setFontHeightInPoints((short) 16);
        font.setBold(true);
        style.setFont(font);
        //创建第二行用来放表头文字
        int rowNum = 1; // 行号
        XSSFRow row1 = sheet.createRow(rowNum++);
        //表头数据,循环将表头数据填充到第二行
        for (int i = 0; i < this.heads.size(); i++) {
            XSSFCell c1 = row1.createCell(i);
            c1.setCellValue(this.heads.get(i));
            c1.setCellStyle(style);
        }
    }


    /**
     * 写入统计的头部
     *
     * @param title 统计的标题
     */
    public void writeCountHeadTitle(String title) {
        XSSFCellStyle style = this.workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER); //居中
        style.setWrapText(true); //自动换行
        style.setFillBackgroundColor(IndexedColors.GREEN.index); //背景颜色
        XSSFFont font = this.workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 16);
        font.setBold(true);
        style.setFont(font);
        int rowNum = 0; // 行号
        XSSFRow row0 = sheet.createRow(rowNum++);
        XSSFCell c00 = row0.createCell(0);
        c00.setCellValue(title); // 写入标题
        c00.setCellStyle(style);
        //设置跨行,前两个参数是需要合并的开始行数和结束行数,后两个是开始列数和结束列数
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, this.countHeads.size() - 1));
        //冻结行数,冻结前两行
        sheet.createFreezePane(0, 2, 0, 2);
    }


    /**
     * 写入头部标题
     */
    public void writeHeadTitle(String title) {
        XSSFCellStyle style = this.workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER); //居中
        style.setWrapText(true); //自动换行
        style.setFillBackgroundColor(IndexedColors.GREEN.index); //背景颜色
        XSSFFont font = this.workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 16);
        font.setBold(true);
        style.setFont(font);
        int rowNum = 0; // 行号
        XSSFRow row0 = sheet.createRow(rowNum++);
        XSSFCell c00 = row0.createCell(0);
        c00.setCellValue(title); // 写入标题
        c00.setCellStyle(style);
        //设置跨行,前两个参数是需要合并的开始行数和结束行数,后两个是开始列数和结束列数
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, this.heads.size() - 1));
        //冻结行数,冻结前两行
        sheet.createFreezePane(0, 2, 0, 2);
    }


    /**
     * 初始化对象
     */
    public AppLotteryExport() {
        this.workbook = new XSSFWorkbook();
        this.heads = new ArrayList<>();
        this.countHeads = new ArrayList<>();
        this.simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.heads.add("用户编号");
        this.heads.add("部门名称");
        this.heads.add("姓名");
        this.heads.add("手机号");
        this.heads.add("抽奖轮数");
        this.heads.add("中奖时间");
        this.heads.add("奖品名称");
        this.heads.add("奖品数量");
        this.heads.add("兑换码");
        this.countHeads.add("序号");
        this.countHeads.add("部门");
        this.countHeads.add("数量");
    }


}
